package com.wsg.lightaccounting.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsg.lightaccounting.Activity.MainActivity;
import com.wsg.lightaccounting.Fragment.CircularFragment;
import com.wsg.lightaccounting.Fragment.LineFragment;
import com.wsg.lightaccounting.Fragment.RecordFragment;
import com.wsg.lightaccounting.Model.AccountBooks;
import com.wsg.lightaccounting.Model.AccountManager;
import com.wsg.lightaccounting.Model.ExpenditureType;
import com.wsg.lightaccounting.Model.IncomeType;
import com.wsg.lightaccounting.Model.Record;
import com.wsg.lightaccounting.Model.RecordManager;
import com.wsg.lightaccounting.R;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by dell-pc on 2016/11/20.
 */

public class RecordAdapter extends BaseAdapter{
    private Context context;
    private List<Record> records;
    private List<IncomeType> incometypes;
    private List<ExpenditureType> expendituretypes;
    public static boolean isEdit = false;

    public static void setIsEdit(boolean isEdit) {
        RecordAdapter.isEdit = isEdit;
    }

    public RecordAdapter(Context context){
        this.context = context;
        setRecords();
        incometypes = RecordManager.incomeTypes;
        expendituretypes = RecordManager.expenditureTypes;
    }

    public void setRecords() {
        this.records = new LinkedList<Record>();
        for (int i = 0; i <RecordManager.records.size() ; i++) {
            if (RecordManager.records.get(i).getNotetype() == RecordFragment.account_book_id) {
                this.records.add(RecordManager.records.get(i));
            }
        }
    }

    @Override
    public int getCount() {
        return records.size();
    }

    @Override
    public Object getItem(int position) {
        return records.get(position);
    }

    @Override
    public long getItemId(int position) {
        return records.get(position).getId();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyItemView itemView;
        if (convertView == null){
            LayoutInflater inflater =  LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.record_layout, null);
            itemView = new   MyItemView((TextView) convertView.findViewById(R.id.expenditure_money),(TextView)convertView.findViewById(R.id.expenditure_type),
                    (TextView)convertView.findViewById(R.id.income_money),(TextView)convertView.findViewById(R.id.income_type),
                    (ImageView)convertView.findViewById(R.id.expenditure_icon),(ImageView)convertView.findViewById(R.id.income_icon));
            convertView.setTag(itemView);
        }
        else{
            itemView = (MyItemView) convertView.getTag();
        }
        if (records.get(position).isExpenditure()) {
            for (int i = 0; i < expendituretypes.size(); i++) {
                if (records.get(position).getType() == expendituretypes.get(i).getId()){
                    RecordManager.getRecordManager(context).setIcon(itemView.expenditure_icon,true,expendituretypes.get(i).getIcon());
                    itemView.expenditure_type.setText(expendituretypes.get(i).getName());
                    break;
                }
            }
            itemView.expenditure_money.setText(String.valueOf(records.get(position).getMoney()));
            itemView.income_icon.setBackground(null);
            itemView.income_type.setText("");
            itemView.income_money.setText("");
        }
        else{
            for (int i = 0; i < incometypes.size(); i++) {
                if (records.get(position).getType() == incometypes.get(i).getId()){
                    RecordManager.getRecordManager(context).setIcon(itemView.income_icon,false,incometypes.get(i).getIcon());
                    itemView.income_type.setText(incometypes.get(i).getName());
                    break;
                }
            }
            itemView.income_money.setText(String.valueOf(records.get(position).getMoney()));
            itemView.expenditure_icon.setBackground(null);
            itemView.expenditure_type.setText("");
            itemView.expenditure_money.setText("");
        }

        if (isEdit){
            convertView.findViewById(R.id.delete).setVisibility(View.VISIBLE);
            convertView.findViewById(R.id.edit).setVisibility(View.VISIBLE);
            convertView.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RecordManager.getRecordManager(context).deleteRecord(records.get(position));
                    records.remove(records.get(position));
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    MainActivity.updateAllUI(context);
                }
            });
        }
        else
        {
            convertView.findViewById(R.id.delete).setVisibility(View.INVISIBLE);
            convertView.findViewById(R.id.edit).setVisibility(View.INVISIBLE);
        }
        return convertView;
    }
    class MyItemView{
        protected TextView expenditure_money,expenditure_type,income_money,income_type;
        protected ImageView expenditure_icon,income_icon;

        public MyItemView(TextView expenditure_money, TextView expenditure_type, TextView income_money, TextView income_type,
                          ImageView expenditure_icon, ImageView income_icon) {
            this.expenditure_money = expenditure_money;
            this.expenditure_type = expenditure_type;
            this.income_money = income_money;
            this.income_type = income_type;
            this.expenditure_icon = expenditure_icon;
            this.income_icon = income_icon;
        }
    }
}
